﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using X23.ApplicationJob.Application.Dto;

namespace X23.ApplicationJob
{
    internal static class StaticDic
    {
        internal static string MysqlConnectionStr = "";
        /// <summary>
        /// 全局静态变量
        /// </summary>
        internal static Dictionary<string, JobAttributeAndTypeDto> JobStaticDic = new();

        /// <summary>
        /// 初始化注册JobAttribute特性
        /// </summary>
        /// <param name="assemblies"></param>
        internal static void IniJobStaticDic(params Assembly[] assemblies)
        {
            var jobAttributeAndTypeDtoList = new List<JobAttributeAndTypeDto>();
            assemblies.ToList().ForEach(item =>
            {
                var attributes = item.DefinedTypes.SelectMany(x => x.DeclaredMethods
                        .Where(x => x.GetCustomAttribute(typeof(JobAttribute), false) != null))
                        .Select(x => new JobAttributeAndTypeDto
                        {
                            JobAttribute = x.GetCustomAttribute(typeof(JobAttribute), false) as JobAttribute,
                            ClassType = x.DeclaringType
                        })
                        .ToList();
                jobAttributeAndTypeDtoList.AddRange(attributes);
            });
            JobStaticDic = jobAttributeAndTypeDtoList.ToDictionary(
                x => x.JobAttribute.Key,
                y => new JobAttributeAndTypeDto
                {
                    ClassType = y.ClassType,
                    JobAttribute = y.JobAttribute
                });
        }
    }
}
